package com.wl.dzt.sys.swagger;

import javax.annotation.Resource;

import io.swagger.annotations.Api;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.swagger.annotations.ApiOperation;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


/**
 * @author wl_code@163.com
 * @version 1.0
 * @date 2018/7/5/005
 * @description swagger2配置类, 自动读取父项目中配置
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Resource
    private SwaggerInfo swaggerInfo;

    /**
     * 构建swaggerApi bean,扫描所有有注解的api
     *
     * @return
     */
    @Bean
    public Docket swaggerApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
//				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any())
//				.apis(RequestHandlerSelectors.withMethodAnnotation(Api.class)).paths(PathSelectors.any())
                .apis(RequestHandlerSelectors.basePackage(swaggerInfo.getBasePackage()))
                .build();
    }


    /**
     * 版本信息
     *
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title(swaggerInfo.getTitle()).description(swaggerInfo.getDescription())
                .version(swaggerInfo.getVersion())
                .contact(new Contact(swaggerInfo.getAuthor(), swaggerInfo.getContact(), swaggerInfo.getEmail()))
                .termsOfServiceUrl(swaggerInfo.getTermsOfServiceUrl()).build();
    }

}
